package com.hutao.medical.controller;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hutao.medical.common.util.CookieUtils;
import com.hutao.medical.common.util.ExcelUtil;
import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.constant.FunctionType;
import com.hutao.medical.pojo.quality.HospitalNote;
import com.hutao.medical.pojo.quality.MedicalQuality;
import com.hutao.medical.pojo.quality.PointsStandard;
import com.hutao.medical.pojo.vo.MedicalAjaxVo;
import com.hutao.medical.pojo.vo.MedicalRecordVo;
import com.hutao.medical.pojo.vo.MedicalSearchVO;
import com.hutao.medical.pojo.vo.QueryMedQuaTotalParam;
import com.hutao.medical.service.BasicDataService;
import com.hutao.medical.service.quality.QualityService;

@Controller
@RequestMapping("/quality")
public class MedQualityController {
	private static Logger log = LoggerFactory.getLogger(MedQualityController.class);

	@Autowired
	private QualityService qualityService;

	@Autowired
	private BasicDataService basicDataService;

	
	/*@RequestMapping("/exportQueryQualityDefectToExcel")
	public void exportQueryQualityDefectToExcel(QueryMedQuaTotalParam param,HttpServletResponse response){
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			response.setContentType("octets/stream");
			String excelName = "病案缺陷统计";
			// 转码防止乱码
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			list = queryQualityDefect(param);
			String title = "病案缺陷统计";
			String[] header = { "甲级项目", "质控数", "乙级项目", "质控数", "丙级项目", "质控数"};
			ExcelUtil.exportExcel(out, title, header, list);
			System.out.println(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	@RequestMapping("/findQuality")
	public String findQuality(MedicalRecordVo medicalRecordVo,Model model) {
		// 科室信息集合
		Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();
		model.addAttribute("officeList", map.get("offices"));
		/*if(StringUtils.isNotEmpty(firstGuid)){
			MedicalRecordVo medicalRecordVo = new MedicalRecordVo();
			medicalRecordVo.setFirstGuid(firstGuid);
			List<Map<String, Object>> qualityList = qualityService.getMedicalQualityFunctionList(medicalRecordVo);
			if(CollectionUtils.isNotEmpty(qualityList)){
				
				for (Map<String, Object> qualityMap : qualityList) {
					System.out.println(qualityMap);
					double point = 0.0f;
					point=  qualityMap.get("points")==null?0.0:(double)qualityMap.get("points");
					qualityMap.put("qcPoints", 100.0-point);
				}
			}
			
			model.addAttribute("qualityList",qualityList);
		}*/
		
		model.addAttribute("medicalRecordVo", medicalRecordVo);
		
		if(medicalRecordVo!=null){
			if(medicalRecordVo.getStartTime()!=null){
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String startTime = simpleDateFormat.format(medicalRecordVo.getStartTime());
				model.addAttribute("startTime", startTime);
			}
			
			if(medicalRecordVo.getEndTime()!=null){
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String endTime = simpleDateFormat.format(medicalRecordVo.getEndTime());
				model.addAttribute("endTime", endTime);
			}
			
			
		}
		

		return "medControl/findQuality";
	}

	@RequestMapping("/operateQua")
	public String operateQua(String firstGuid, Model model) {
		model.addAttribute("firstGuid", firstGuid);

		return "medControl/operateQua";
	}
    //质控首页
	@RequestMapping("/qualityHomepage")
	public String qualityHomepage(Model model) {
		Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();
		model.addAttribute("officeList", map.get("offices"));
		model.addAttribute("outHospitalTypeList", map.get("outHospitalList"));
		return "medControl/qualityHomepage";
	}
	/*//质控首页详情
	@RequestMapping("/qualityHomepageDetail")
		public String qualityHomepageDetail(String firstGuid, Model model,HttpServletRequest request) {		
		String currentUser = CookieUtils.getCookieValue(request, "userName");
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid);
		List<Map<String,Object>> qualityList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.firstQuality);
		model.addAttribute("medicalRecord", hospitalNote);
		model.addAttribute("qualityList", qualityList);
		model.addAttribute("operator", currentUser);
		return "medControl/qualityHomepage_detail";
	}*/
	 //质控首页统计
	@RequestMapping("/qualityHomepageSearch")
	    public String qualityHomepageSearch(Model model) {
		Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();
		model.addAttribute("officeList", map.get("offices"));
	    return "medControl/qualityHomepageSearch";
	}
	 //质控首页统计详情
	@RequestMapping("/qualityHomepageSearchDetail")
	   public String qualityHomepageSearchDetail(Model model) {
	   return "medControl/qualityHomepageSearch_detail";
	}	
	/**
	 * 病案质量统计
	 * 
	 * @return
	 */
	@RequestMapping("/medQuaStatis")
	public String medQuaStatis(Model model) {
		Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();
		model.addAttribute("officeList", map.get("offices"));
		return "medControl/medQuaStatis";
	}
	
	/**
	 * 病案质控率
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMedRecordQualityRate")
	public Result getMedRecordQualityRate(MedicalAjaxVo medicalAjaxVo){
		Result result = null;
		try {
			Map<String,Object> qualityRateMap = qualityService.getMedRecordQualityRate(medicalAjaxVo);
			result = new Result(true,"查询成功!",qualityRateMap);
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false,"查询失败!");
		}
		return result;
	}

	
	@RequestMapping("/detailControl")
	public String detailControl(String firstGuid, MedicalSearchVO medicalSearchVO,Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		String currentUser = CookieUtils.getCookieValue(request, "userName");
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.quality);

		if(medicalSearchVO!=null){
			if(medicalSearchVO.getOutHospitalStartDate()!=null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				String startTime = format.format(medicalSearchVO.getOutHospitalStartDate());
				model.addAttribute("startTime",startTime);
			}
			
			if(medicalSearchVO.getOutHospitalEndDate()!=null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				String endTime = format.format(medicalSearchVO.getOutHospitalEndDate());
				model.addAttribute(endTime);
				model.addAttribute("endTime",endTime);
			}
			
			
			if(StringUtils.isNotEmpty(medicalSearchVO.getName())){
				model.addAttribute("patientName",  URLDecoder.decode(medicalSearchVO.getName(), "UTF-8"));
			
			}
		}
		List<PointsStandard> pointsStandardList = qualityService.getFirstStandard();
		
		List<Map<String,Object>> qualityList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.quality);
		model.addAttribute("firstStands", pointsStandardList);
		model.addAttribute("medicalRecord", hospitalNote);
		model.addAttribute("operator", currentUser);
		model.addAttribute("qualityList", qualityList);
		return "medControl/detailController";
	}
	
	@RequestMapping("/detailControl1")
	public String detailControl1(String firstGuid, MedicalSearchVO medicalSearchVO,Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		String currentUser = CookieUtils.getCookieValue(request, "userName");
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.quality);

		if(medicalSearchVO!=null){
			if(medicalSearchVO.getOutHospitalStartDate()!=null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				String startTime = format.format(medicalSearchVO.getOutHospitalStartDate());
				model.addAttribute("startTime",startTime);
			}
			
			if(medicalSearchVO.getOutHospitalEndDate()!=null){
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				String endTime = format.format(medicalSearchVO.getOutHospitalEndDate());
				model.addAttribute(endTime);
				model.addAttribute("endTime",endTime);
			}
			
			
			if(StringUtils.isNotEmpty(medicalSearchVO.getName())){
				model.addAttribute("patientName",  URLDecoder.decode(medicalSearchVO.getName(), "UTF-8"));
			
			}
		}
		List<PointsStandard> pointsStandardList = qualityService.getFirstStandard();
		
		List<Map<String,Object>> qualityList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.quality);
		model.addAttribute("firstStands", pointsStandardList);
		model.addAttribute("medicalRecord", hospitalNote);
		model.addAttribute("operator", currentUser);
		model.addAttribute("qualityList", qualityList);
		return "medControl/detailController1";
	}
	
	@RequestMapping("/ajaxDetailControl")
	@ResponseBody
	public Result ajaxDetailControl(String firstGuid, HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<>();
		try {
			HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.quality);
			List<PointsStandard> pointsStandardList = qualityService.getFirstStandard();
			
			List<Map<String,Object>> qualityList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.quality);
			map.put("medicalRecord", hospitalNote);
			map.put("qualityList", qualityList);
			
			return new Result(true, "查询成功!", map, 200);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "系统异常!");
		}
		
	}
	
	

	@RequestMapping("/updateMedQua")
	public String updateMedQua(String firstGuid, Integer mqcId, Model model) {
		List<PointsStandard> pointsStandardList = qualityService.getFirstStandard();
		List<Map<String,Object>> qualityMapList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.quality);
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.quality);
		model.addAttribute("firstStands", pointsStandardList);
		model.addAttribute("qualityMapList", qualityMapList);
		model.addAttribute("hospitalNote", hospitalNote);
		model.addAttribute("firstGuid", firstGuid);
		return "medControl/updateMedQua";
	}

	

	@ResponseBody
	@RequestMapping("/getQualityProjectList")
	public List<PointsStandard> getQualityProjectList(Integer parentId) {
		return qualityService.getStandardList(parentId);
	}

	@ResponseBody
	@GetMapping("/ajax_getQualityProjectList")
	public Result ajax_getQualityProjectList(Integer parentId) {
		try {
			List<PointsStandard> pointsStandards = qualityService.getStandardList(parentId);
			String jsonStr = JsonUtils.objectToJson(pointsStandards);
			return new Result(true, "查询成功!!", jsonStr, 200);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("-------移动端查询质控二级项目异常!------" + e);
			return new Result(false, "系统异常!", null, 500);
		}
	}
	
	
	@ResponseBody
	@RequestMapping("/getPointsStandardById")
	public PointsStandard getPointsStandardById(Integer id) {
		return qualityService.getPointsStandard(id);
	}


	
	@ResponseBody
	@RequestMapping("/addMedQualityCont")
	public Result addMedQualityCont(MedicalQuality medicalQuality,String firstGuid,HttpServletRequest request) {
		String currentUser = CookieUtils.getCookieValue(request, "userName");
		try {
			qualityService.addMedicalQuality(medicalQuality,firstGuid,currentUser,FunctionType.quality);
			List<Map<String,Object>> qualityList=qualityService.getMedQualityByFirstGuidAndFunType(firstGuid,FunctionType.quality);
			return new Result(true,"质控信息添加成功!",qualityList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存质控信息失败!");
		}
	}
	
	@ResponseBody
	@PostMapping("/ajaxAddMedQualityCont")
	public Result ajaxAddMedQualityCont(String firstGuid,HttpServletRequest request) {
		try {
			HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.quality);
			
			 MedicalQuality medicalQuality = new MedicalQuality();
			 String medControlDesc = request.getParameter("medControlDesc");
			 String qcDockPoints = request.getParameter("qcDockPoints");
			 String firstProId = request.getParameter("firstProId");
			 String secondProId = request.getParameter("secondProId");
		     String thirdProId = request.getParameter("thirdProId");
			 String userName = request.getParameter("userName");
		     
		     medicalQuality.setMedControlDesc(medControlDesc);
		     medicalQuality.setQcDockPoints(qcDockPoints != null? Float.parseFloat(qcDockPoints):0);
		     medicalQuality.setFirstProId(firstProId!=null? Integer.parseInt(firstProId):null);
		     medicalQuality.setSecondProId(secondProId!=null? Integer.parseInt(secondProId):null);
		     medicalQuality.setThirdProId(thirdProId!=null? Integer.parseInt(thirdProId):null);
		     
			qualityService.addMedicalQuality(medicalQuality, firstGuid,userName, FunctionType.quality);
			return new Result(true, "质控信息添加成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存质控信息失败!");
		}
	}
	
	
	/**
	 * 添加首页质控信息
	 * @param medicalQuality
	 * @param firstGuid
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addFirstMedQuality")
	public Result addFirstMedQuality(MedicalQuality medicalQuality,String firstGuid,HttpServletRequest request) {
		String currentUser = CookieUtils.getCookieValue(request, "userName");
		try {
			qualityService.addMedicalQuality(medicalQuality,firstGuid,currentUser,FunctionType.firstQuality);
			return new Result(true, "质控信息添加成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存质控信息失败!");
		}
	}

//	@ResponseBody
//	@RequestMapping("/updateMedQualityCont")
//	public Result updateMedQualityCont(MedQualityControl medQualityControl) {
//		try {
//			// medQualityControl.setControlTime(new Date());
//			qualityService.updateMedQuality(medQualityControl);
//			return new Result(true, "质控信息修改成功!");
//		} catch (Exception e) {
//			// TODO: handle exception
//			return new Result(false, "质控信息修改失败!");
//		}
//	}

	@RequestMapping("/qualitySearch")
	public String qualitySearch(MedicalSearchVO medicalSearchVO, Model model) {
		try {
			Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();
			// 科室信息集合
			String name =null;
			
			if(medicalSearchVO!=null){
				if(StringUtils.isNotEmpty(medicalSearchVO.getName())  && !medicalSearchVO.getName().equals("undefined")){
					name= URLDecoder.decode(medicalSearchVO.getName(), "UTF-8");
					model.addAttribute("name", name);
				}
				
				if(medicalSearchVO.getOutHospitalStartDate()!=null){
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
					String startTime = simpleDateFormat.format(medicalSearchVO.getOutHospitalStartDate());
					model.addAttribute("startTime", startTime);
				}
				
				if(medicalSearchVO.getOutHospitalEndDate()!=null){
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
					String endTime = simpleDateFormat.format(medicalSearchVO.getOutHospitalEndDate());
					model.addAttribute("endTime", endTime);
				}
				
				
			}
			model.addAttribute("name", name);
			model.addAttribute("officeList", map.get("offices"));
			model.addAttribute("outHospitalTypeList", map.get("outHospitalList"));
			model.addAttribute("medicalSearchVO", medicalSearchVO);
			

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "medControl/qualitySearch";
	}

//	@ResponseBody
//	@RequestMapping("/getQualityConList")
//	public List<MedQualityControl> getQualityConList() {
//		return qualityService.getQualityConList();
//	}

//	@RequestMapping("/showDetail")
//	public String getMedQualityControlById(Integer id, Model model) {
//		MedQualityControl medQualityControl = qualityService.getQuality(id);
//		model.addAttribute("quality", medQualityControl);
//		return "medControl/showDetail";
//	}

//	@ResponseBody
//	@RequestMapping("/getUnQuaList")
//	public Map<String, Object> getUnQuaList(Params params) {
//		Map<String, Object> map = new HashMap<>();
//
//		try {
//			List<Map<String, Object>> list = qualityService.getUnQuaList(params);
//			map.put("unQuaList", list);
//			int count = qualityService.getUnQuaListCount(params);
//			System.out.println("-----------------count=" + count + "-------------------------");
//			map.put("count", count);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return map;
//	}

//	@ResponseBody
//	@RequestMapping("/getMedQualityList")
//	public Map<String, Object> getMedQualityList(Params params) {
//		Map<String, Object> map = new HashMap<>();
//		try {
//			params.setStatus(2);
//			System.out.println(params.getName() + "--" + params.getMrId() + "--" + params.getOnlyId() + "--"
//					+ params.getStatus() + "--" + params.getStartTime() + "--" + params.getEndTime());
//			List<Map<String, Object>> quaList = qualityService.getQuaList(params);
//			int count = qualityService.getQuaListCount(params);
//			map.put("quaList", quaList);
//			map.put("count", count);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return map;
//
//	}

//	@ResponseBody
//	@RequestMapping("/getSingleMedList")
//	public List<Map<String, Object>> getSingleMedList(String firstGuid) {
//		List<Map<String, Object>> medList = new ArrayList<>();
//		try {
//			medList = qualityService.getSingleMedList(firstGuid);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return medList;
//	}


	@ResponseBody
	@RequestMapping("/updateQua")
	public String updateQua(MedicalQuality MedicalQuality, HttpServletRequest request) {
		try {
			
			qualityService.updateMedQuaCon(MedicalQuality);

		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	/**
	 * 统计病案质控
	 * 
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMedQuaStaticMapList")
	public Map<String, Object> getMedQuaStaticMapList(MedicalRecordVo medicalRecordVo) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<Map<String, Object>> list = qualityService.getMedQuaStaticMapList(medicalRecordVo);
			map.put("list", list);
			Map<String, List<Map<String, Object>>> basicMap = basicDataService.getBaseInfoMap();
			List<Map<String, Object>> officeList = basicMap.get("offices");
			map.put("count", officeList == null ? 0 : officeList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping("/exportMedQuaExcel")
	public void exportMedQuaExcel(MedicalRecordVo medicalRecordVo, HttpServletResponse response) {
		List<Map<String, Object>> list = new ArrayList<>();

		try {
			response.setContentType("octets/stream");
			String excelName = "质控统计信息";
			// 转码防止乱码
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			//System.out.println(medicalRecordVo.getStartTime());
			list = qualityService.getMedQuaStaticMapList(medicalRecordVo);
			String title = "质控统计信息";
			String[] header = { "科室名称", "质控数", "甲级质控数", "甲级质控率(%)", "乙级质控数", "乙级质控率(%)", "丙级质控数", "丙级质控率(%)", "扣分总数" };
			ExcelUtil.exportExcel(out, title, header, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询
	 * 
	 * @return
	 */
//	@ResponseBody
//	@RequestMapping("/totalMedQuaMsg")
//	public Result totalMedQuaMsg(QueryMedQuaTotalParam queryMedQuaTotalParam) {
//		Result result = null;
//		List<Map<String, Object>> medQuaMsgList = qualityService.totalMedQuaMsg(queryMedQuaTotalParam);
//		if (medQuaMsgList.size() > 0) {
//			result = new Result(true, "查询成功!", medQuaMsgList);
//		} else {
//			result = new Result(false, "暂无数据!", medQuaMsgList);
//		}
//		return result;
//	}

//	@RequestMapping("/exportMedDefaultExcel")
//	public void exportMedDefaultExcel(HttpServletResponse response, QueryMedQuaTotalParam queryMedQuaTotalParam) {
//		List<Map<String, Object>> scanWorkMapList = new ArrayList<>();
//		try {
//			response.setContentType("octets/stream");
//			String excelName = "病案缺陷";
//			// 转码防止乱码
//			response.addHeader("Content-Disposition",
//					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
//			// 通过response获取文件输出流
//			OutputStream out = response.getOutputStream();
//			// 查询数据
//			scanWorkMapList = qualityService.exportMedDefaultExcel(queryMedQuaTotalParam);
//			String title = "病案缺陷";
//			String[] header = { "评价项目id", "评价项目", "缺陷数" };
//			ExcelUtil.exportExcel(out, title, header, scanWorkMapList);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	@ResponseBody
	@RequestMapping("/selectQualityListByKeywords")
	public Result selectQualityListByKeywords(MedicalRecordVo medicalRecordVo) {
		Result result = null;
		try {
			Map<String, Object> map = new HashMap<>();
			List<Map<String, Object>> qualityList = qualityService.getMedicalQualityFunctionList(medicalRecordVo);
			int totalCount = qualityService.getMedicalQualityFunctionCount(medicalRecordVo);
			map.put("qualityList", qualityList);
			map.put("totalCount", totalCount);
			if (qualityList.size() > 0) {
				result = new Result(true, "查询成功!", map);
			} else {
				result = new Result(false, "暂无数据!", map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false, "查询质控信息出现异常:ex=" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("/exportfindQualitExcel")
	public void exportfindQualitExcel(HttpServletResponse response, MedicalRecordVo medicalRecordVo) {
		List<Map<String, Object>> scanWorkMapList = new ArrayList<>();
		try {
			response.setContentType("octets/stream");
			String excelName = "质控查询";
			// 转码防止乱码
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
			// 通过response获取文件输出流
			OutputStream out = response.getOutputStream();
			// 查询数据
			scanWorkMapList = qualityService.exportQualityListToExcel(medicalRecordVo);
			String title = "质控查询";
			String[] header = { "病案号", "姓名", "证件号码", "出院科室", "病案等级", "质控分数" };
			ExcelUtil.exportExcel(out, title, header, scanWorkMapList);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ResponseBody
	@RequestMapping("/deleteQuality")
	public Result deleteQuality(Integer mqcId,Integer mqId) {
		Result result = null;
		try {
			qualityService.deleteQuality(mqcId,mqId);
			result = new Result(true, "删除成功!");
		} catch (Exception e) {
			result = new Result(false, "删除失败!");
		}
		return result;
	}

	@ResponseBody
	@RequestMapping("/ajaxDeleteQuality")
	public Result ajaxDeleteQuality(Integer mqcId,Integer mqId) {
		Result result = null;
		try {
			qualityService.deleteQuality(mqcId,mqId);
			result = new Result(true, "删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false, "删除失败!");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/completeQuality")
	public Result completeQuality(String uniqueKey, HttpServletRequest request) {
		Result result = null;
		try {
			String currentUser = CookieUtils.getCookieValue(request, "userName");
			
			
			qualityService.completeQuality(uniqueKey, currentUser);
			result = new Result(true, "质控完成!");
		} catch (Exception e) {
			result = new Result(false, "质控失败!,出现异常:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("/medQualityCheckPage")
	public String medQualityCheckPage(Model model, Integer mqcId) {
		Map<String, Object> map = qualityService.selectQualityByMqcId(mqcId);
		model.addAttribute("map", map);

		return "medControl/medQualityCheckPage";
	}

//	@RequestMapping("/addMedQualityRepair")
//	public String addMedQualityRepair(MedQuaConRepairlog medQuaConRepairlog, Model model, HttpServletRequest request) {
//		String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
//		qualityService.addMedQualityRepair(medQuaConRepairlog);
//		medQuaConRepairlog.setCheckMan(currentUser);
//		return updateMedQua(medQuaConRepairlog.getFirstLeverMedicalGuid(), medQuaConRepairlog.getMedQualityControlId(),
//				model);
//	}

//	@ResponseBody
//	@RequestMapping("/selectMedRepairByKeyWords")
//	public Result selectMedRepairByKeyWords(MedicalRecordVo medicalRecordVo) {
//		Result result = null;
//		try {
//			Map<String, Object> map = new HashMap<>();
//			List<MedQuaConRepairlog> list = qualityService.selectMedRepairByKeyWords(medicalRecordVo);
//			int count = qualityService.selectMedRepairCountByKeyWords(medicalRecordVo);
//			map.put("list", list);
//			map.put("count", count);
//			if (list.size() > 0) {
//				result = new Result(true, "查询成功!", map);
//			} else {
//				result = new Result(false, "暂无数据!", null);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			result = new Result(false, "查询失败:ex{}=" + e.getMessage());
//		}
//		return result;
//	}

//	@RequestMapping("/exportMedRepairToExcel")
//	public void exportMedRepairToExcel(HttpServletResponse response, MedicalRecordVo medicalRecordVo) {
//		List<Map<String, Object>> mapList = new ArrayList<>();
//		try {
//			response.setContentType("octets/stream");
//			String excelName = "病案返修";
//			// 转码防止乱码
//			response.addHeader("Content-Disposition",
//					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
//			OutputStream out = response.getOutputStream();
//			System.out.println(medicalRecordVo.getStartTime());
//			mapList = qualityService.exportMedRepairToExcel(medicalRecordVo);
//			String title = "病案返修";
//			String[] header = { "病案号", "姓名", "修改时间", "返修前内容", "返修后内容", "返修者" };
//			ExcelUtil.exportExcel(out, title, header, mapList);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	@ResponseBody
	@RequestMapping("/getUnQualityMedList")
	public Result getUnQualityMedList(MedicalAjaxVo medicalAjaxVo) {
		Result result = null;
		try {
			// Map<String,Object> map=MapUtil.objectToMap(medicalAjaxVo);
			// String userJson = HttpClientUtil.doGet(ajaxQueryMedical, map);
			// result = JSONObject.parseObject(userJson, Result.class);
		} catch (Exception e) {
		}
		return result;
	}

	@ResponseBody
	@RequestMapping("/checkQua")
	public String checkQua(Integer mqcId, Integer status,HttpServletRequest request) {
		try {
			String currentUser = CookieUtils.getCookieValue(request, "userName");
			qualityService.checkQua(mqcId, status,currentUser);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping("/getFirstMedQuaLityList")
	public Result getFirstMedQuaLityList(MedicalRecordVo medicalRecordVo) {
		Result result = null;
		try {
			medicalRecordVo.setFunctionTypeId(FunctionType.firstQuality);
			Map<String, Object> map = new HashMap<>();
			List<Map<String,Object>> list = qualityService.getFirstMedQuaLityList(medicalRecordVo);
			
			int count = qualityService.getFirstMedQuaLityCount(medicalRecordVo);
			map.put("list", list);
			map.put("count", count);
			if (list.size() > 0) {
				result = new Result(true, "查询成功!", map);
			} else {
				result = new Result(false, "暂无数据!", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false, "查询失败:ex{}=" + e.getMessage());
		}
		return result;
	}
	
	@ResponseBody
	@GetMapping("/ajax_getFirstStandard")
	public Result getFirstStandard() {
		try {
			List<PointsStandard> pointsStandardList = qualityService.getFirstStandard();
			String jsonStr = JsonUtils.objectToJson(pointsStandardList);
			return new Result(true, "查询成功!", jsonStr, 200);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "系统异常!", null, 500);
		}
	}
	
	@RequestMapping("/exportQualityErrorExcel")
	public void exportQualityErrorExcel(QueryMedQuaTotalParam param,HttpServletResponse response,HttpServletRequest request) {
		try {
			response.setContentType("octets/stream");
			String excelName = "病案质控统计详情";
			// 转码防止乱码
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
			// 通过response获取文件输出流
			OutputStream out = response.getOutputStream();
			
		
			List<Map<String,Object>> list = qualityService.queryQualityDefect(param);
		
			
			
			
			
			String title = excelName;
			String[] header = {"科室名称","扣分项总量","扣分项","扣分项数量","ID号","住院次数","错误描述" };
			//ExcelUtil.exportExcel(out, title,  header, list);
			qualityService.generateQualityErrorExcel(out, title, header, list);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	
	@ResponseBody
	@RequestMapping("/queryQualityDefect")
	public Result queryQualityDefect(QueryMedQuaTotalParam param){
		Result result = null;
		try {
			List<Map<String,Object>> list = qualityService.queryQualityDefect(param);
			System.out.println(param);
			result = new Result(true,"查询成功!",list);
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false,"查询失败");
		}
		return result;
	}
	
	
	
	

}
